package com.practise.rabbitmqdemo.controller;

import com.alibaba.fastjson.JSON;
import com.practise.rabbitmqdemo.config.DeadTTLDirectRabbitConfig;
import com.practise.rabbitmqdemo.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping(value = "ttl/dead")
@Api(value = "Dead TTL DirectMsg Controller", tags = {"测试死信队列接口"})
public class DeadTTLDirectMsgController {

    @Autowired
    RabbitTemplate rabbitTemplate;

    @ApiOperation(value = "发送消息TTLQ2测试死信队列", httpMethod = "POST")
    @RequestMapping(value = "sendToTTLQ2")
    public void send1(@RequestBody @ApiParam(name = "User", value = "用户类", required = true) User user) {
        log.info("发送json消息体 TTLQ2测试死信队列 msg : {}",user);
        rabbitTemplate.convertAndSend(DeadTTLDirectRabbitConfig.TTL_EXCHANGE_2,DeadTTLDirectRabbitConfig.TTL_ROUTKEY_2, JSON.toJSONString(user));
    }


    @ApiOperation(value = "发送消息TTLQ2测试死信队列", httpMethod = "POST")
    @RequestMapping(value = "sendStrToTTLQ2")
    public void send2(String msg) {
        log.info("发送string消息 TTLQ2测试死信队列 msg : {}",msg);
        rabbitTemplate.convertAndSend(DeadTTLDirectRabbitConfig.TTL_EXCHANGE_2,DeadTTLDirectRabbitConfig.TTL_ROUTKEY_2,msg);
    }

    @ApiOperation(value = "发送消息TTLQ3测试死信队列（插件方式", httpMethod = "POST")
    @RequestMapping(value = "sendStrToTTLQ3")
    public void send3(String msg,int expTime) {
        log.info("发送string消息 TTLQ3测试死信队列 msg : {}",msg);
        rabbitTemplate.convertAndSend(DeadTTLDirectRabbitConfig.DEAD_EXCHANGE_3,DeadTTLDirectRabbitConfig.DEAD_ROUTKEY_3,msg,
                message -> {
                     message.getMessageProperties().setDelay(expTime * 1000);
                     return message;
                });
    }
}
